<template>
  <div class="scct">
    <el-form :model="formInline" label-position="left" class="xtcm" inline>
        <el-form-item label="错误文件">
          <Select 
            ref="selectTable"
            :selectTableColumnData="selectTableColumnData" 
            placeholder="选择错误文件" 
            optionKey="fileName" 
            width="55%"
            @changeFdata="changeFdata"
            @changeFobj="changeFobj"
            :hasRemoteMethod="true"
            paramsKey="condition"
            url="/tos/tediship/getErrorFileMessage"
            method='GET'
            :params="{condition:''}"
          />
        </el-form-item>
        
        <el-form-item >
          <el-button type="primary" @click="getErrorFile">下载</el-button>
        </el-form-item>
        <el-form-item >
          <el-button type="primary" @click="deleteErrorFile">删除</el-button>
        </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Select from '@/components/Select/import-ship-cntr'
import api from '@/api/modules/EDI/index.js'
export default {
  name: 'ediErrorFIle',
  components: { Select },
  data() {
    return {
      formInline: {
        fileName: '',
        fileType: ''
      },
      tableData: [],
      selectTableColumnData: [
        { label: '文件名称', props: 'fileName', width: 600 },
        { label: '解析时间', props: 'recTim', width: 200 },
        { label: '文件类型', props: 'fileType', width: 100 }
      ],
      form: {
        imgSavePath: ''
      }
    }
  },
  mounted() {
    // this.getSelectList()
  },
  methods: {
    changeFdata(text) {
      this.formInline.fileName = text
    },
    changeFobj(text) {
      console.log(text);
      this.formInline.fileType = text[0].fileType
    },
    /**
     * 下载错误文件
     */
    async getErrorFile() {
      let res
      if (this.formInline.fileType === '进口船图') {
        res = await api.reqDownloadCtErrorList({ fileName: this.formInline.fileName })
      } else if (this.formInline.fileType === '出口清单') {
        res = await api.reqDownloadQdErrorList({ fileName: this.formInline.fileName })
      }
      const link = document.createElement('a')
      link.style.display = 'none'
      link.href = URL.createObjectURL(new Blob([res]), { type: 'application/text/plain;charset=GB2312' })
      const fileName = this.formInline.fileName
      link.setAttribute('download', `${fileName}`)
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link)
    },
    deleteErrorFile() {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (this.formInline.fileType === '进口船图') {
          api.reqDeleteCtErrorList({ fileName: this.formInline.fileName }).then(res => {
            if (res.code === '00000000') {
              this.$message({
                message: '删除成功',
                type: 'success'
              });
            } else {
              this.$message({
                message: '删除失败',
                type: 'warning'
              });
            }
            this.$refs.selectTable.getTableData('')
            this.$refs.selectTable.selectLabel = ''
          })
        } else if (this.formInline.fileType === '出口清单') {
          api.reqDeleteQdErrorList({ fileName: this.formInline.fileName }).then(res => {
            if (res.code === '00000000') {
              this.$message({
                message: '删除成功',
                type: 'success'
              });
            } else {
              this.$message({
                message: '删除失败',
                type: 'warning'
              });
            }
            this.$refs.selectTable.getTableData('')
          })
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  }
}
</script>

<style lang="less" scoped>
.scct {
  padding: 10px 20px;
  height: calc(100vh - 94px);
  .xtcm {
    width: 80%;
  }
}
/deep/ .el-card__body {
    padding: 0 20px;
}

</style>